import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  base: process.env.NODE_ENV === 'production' ? '/tggroup/' : '/', // 生产环境使用/tggroup/，开发环境使用根路径
  server: {
    port: 3000,
    open: true,
    host: true, // 允许外部访问
    hmr: {
      overlay: true // 显示错误覆盖层
    },
    proxy: {
      '/api': {
        // target: 'http://127.0.0.1:5001',
        target: 'http://154.12.93.134:5001',
        // target: 'http://tgissue.com/api',
        changeOrigin: true,
        secure: false,
        timeout: 600000, // 10分钟超时
        proxyTimeout: 600000, // 代理超时
        rewrite: (path) => path.replace(/^\/api/, ''),
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('proxy error', err);
          });
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('Sending Request to the Target:', req.method, req.url);
            // 设置请求超时
            proxyReq.setTimeout(600000);
          });
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('Received Response from the Target:', proxyRes.statusCode, req.url);
            // 设置响应超时
            proxyRes.setTimeout(600000);
          });
        }
      }
    }
  },
  resolve: {
    alias: {
      '@': '/src'
    }
  },
  css: {
    devSourcemap: true // CSS源码映射，便于调试
  }
})
